//
// Camera.cpp for Camera in /tmp/noboa-y-global/yn
//
// Made by maxime marie
// Login   <marie_m@epitech.net>
//
// Started on  Fri Apr 20 14:12:58 2012 maxime marie
// Last update Wed May  2 17:35:14 2012 youri noboa
//

#include	<cmath>
#include	"Camera.hh"

Camera::Camera(void)
  : Orientation(0.0f, 1.0f, 0.0f), Focalisation(0.0f, 0.0f, 0.0f), Position(0.0f, 0.0f, 900.0f)
{
}

void		Camera::initialize(void)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(70.f, 800.f/600.0f, 1.0f, 10000.0f);
  gluLookAt(Position.x, Position.y, Position.z,
	    Focalisation.x, Focalisation.y, Focalisation.z,
	    Orientation.x, Orientation.y, Orientation.z);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
}

void		Camera::update(const gdl::GameClock &gameClock,
			       gdl::Input &input)
{
  (void)gameClock;
  if (input.isKeyDown(gdl::Keys::A))
    Position.y += 100;
  if (input.isKeyDown(gdl::Keys::D))
    Position.y -= 100;
  if (input.isKeyDown(gdl::Keys::W))
    Position.z -= 100;
  if (input.isKeyDown(gdl::Keys::S))
    Position.z += 100;
  if (input.isKeyDown(gdl::Keys::Q))
    RotationY(10.0f);
  if (input.isKeyDown(gdl::Keys::E))
    RotationY(-10.0f);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(100.f, 800.f/600.0f, 1.0f, 10000.0f);
  gluLookAt(Position.x, Position.y, Position.z,
	    Focalisation.x, Focalisation.y, Focalisation.z,
	    Orientation.x, Orientation.y, Orientation.z);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void		Camera::RotationY(float angle)
{
  angle = angle / 180 * M_PI;
  Position.x = ((Position.x - Focalisation.x) * cosf(angle) -
		(Position.z - Focalisation.z) * sinf(angle)) + Focalisation.x;
  Position.z = ((Position.x - Focalisation.x) * sinf(angle) +
		(Position.z - Focalisation.z) * cosf(angle)) + Focalisation.z;
}
